/*
 * Copyright (C) 2025 ameise <ameise.wang@gmail.com> - All Rights Reserved
 *
 * This file is part of e3log.
 *
 * e3log is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * e3log is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with e3log. If not, see <https://www.gnu.org/licenses/>.
 */

package e3log

import (
	"fmt"
	"gitee.com/ameise84/e3lock"
	"github.com/fatih/color"
	"os"
)

var _gLogger *Logger

func init() {
	w := NewConsoleWriter(os.Stdout)
	_gLogger = NewLogger(w)
	e3lock.SetLogger(_gLogger)
}

func Sugar(l *Logger) {
	_gLogger = l
	e3lock.SetLogger(l)
}

func ColorMsg(level Level, color *color.Color, msg string) *LogEvent {
	out := false
	switch level {
	case LevelError:
		out = _gLogger.outErrStack
	case LevelFatal:
		out = true
	default:
	}
	return _gLogger.event(_gLogger.skip+1, level, color, msg, out)
}

func Msg(level Level, msg string) *LogEvent {
	out := false
	switch level {
	case LevelError:
		out = _gLogger.outErrStack
	case LevelFatal:
		out = true
	default:
	}
	return _gLogger.event(_gLogger.skip+1, level, nil, msg, out)
}

func Trace(arg any) {
	_gLogger.event(_gLogger.skip+1, LevelTrace, nil, fmt.Sprintf("%v", arg), false).Println()
}

func Debug(arg any) {
	_gLogger.event(_gLogger.skip+1, LevelDebug, nil, fmt.Sprintf("%v", arg), false).Println()
}

func Info(arg any) {
	_gLogger.event(_gLogger.skip+1, LevelInfo, nil, fmt.Sprintf("%v", arg), false).Println()
}

func Warn(arg any) {
	_gLogger.event(_gLogger.skip+1, LevelWarn, nil, fmt.Sprintf("%v", arg), false).Println()
}

func Error(arg any) {
	_gLogger.event(_gLogger.skip+1, LevelError, nil, fmt.Sprintf("%v", arg), _gLogger.outErrStack).Println()
}

func Fatal(arg any) {
	_gLogger.event(_gLogger.skip+1, LevelFatal, nil, fmt.Sprintf("%v", arg), true).Println()
}

func TracePrintf(format string, args ...any) {
	_gLogger.event(_gLogger.skip+1, LevelTrace, nil, fmt.Sprintf(format, args...), false).Println()
}

func DebugPrintf(format string, args ...any) {
	_gLogger.event(_gLogger.skip+1, LevelDebug, nil, fmt.Sprintf(format, args...), false).Println()
}

func InfoPrintf(format string, args ...any) {
	_gLogger.event(_gLogger.skip+1, LevelInfo, nil, fmt.Sprintf(format, args...), false).Println()
}

func WarnPrintf(format string, args ...any) {
	_gLogger.event(_gLogger.skip+1, LevelWarn, nil, fmt.Sprintf(format, args...), false).Println()
}

func ErrorPrintf(format string, args ...any) {
	_gLogger.event(_gLogger.skip+1, LevelError, nil, fmt.Sprintf(format, args...), _gLogger.outErrStack).Println()
}

func FatalPrintf(format string, args ...any) {
	_gLogger.event(_gLogger.skip+1, LevelFatal, nil, fmt.Sprintf(format, args...), true).Println()
}

func TraceObj(obj LogMarshaller, arg any) {
	_gLogger.event(_gLogger.skip+1, LevelTrace, nil, fmt.Sprintf("%v", arg), false).Object(obj).Println()
}

func DebugObj(obj LogMarshaller, arg any) {
	_gLogger.event(_gLogger.skip+1, LevelDebug, nil, fmt.Sprintf("%v", arg), false).Object(obj).Println()
}

func InfoObj(obj LogMarshaller, arg any) {
	_gLogger.event(_gLogger.skip+1, LevelInfo, nil, fmt.Sprintf("%v", arg), false).Object(obj).Println()
}

func WarnObj(obj LogMarshaller, arg any) {
	_gLogger.event(_gLogger.skip+1, LevelWarn, nil, fmt.Sprintf("%v", arg), false).Object(obj).Println()
}

func ErrorObj(obj LogMarshaller, arg any) {
	_gLogger.event(_gLogger.skip+1, LevelError, nil, fmt.Sprintf("%v", arg), _gLogger.outErrStack).Object(obj).Println()
}

func FatalObj(obj LogMarshaller, arg any) {
	_gLogger.event(_gLogger.skip+1, LevelFatal, nil, fmt.Sprintf("%v", arg), true).Object(obj).Println()
}

func TraceObjPrintf(obj LogMarshaller, format string, args ...any) {
	_gLogger.event(_gLogger.skip+1, LevelTrace, nil, fmt.Sprintf(format, args...), false).Object(obj).Println()
}

func DebugObjPrintf(obj LogMarshaller, format string, args ...any) {
	_gLogger.event(_gLogger.skip+1, LevelDebug, nil, fmt.Sprintf(format, args...), false).Object(obj).Println()
}

func InfoObjPrintf(obj LogMarshaller, format string, args ...any) {
	_gLogger.event(_gLogger.skip+1, LevelInfo, nil, fmt.Sprintf(format, args...), false).Object(obj).Println()
}

func WarnObjPrintf(obj LogMarshaller, format string, args ...any) {
	_gLogger.event(_gLogger.skip+1, LevelWarn, nil, fmt.Sprintf(format, args...), false).Object(obj).Println()
}

func ErrorObjPrintf(obj LogMarshaller, format string, args ...any) {
	_gLogger.event(_gLogger.skip+1, LevelError, nil, fmt.Sprintf(format, args...), _gLogger.outErrStack).Object(obj).Println()
}

func FatalObjPrintf(obj LogMarshaller, format string, args ...any) {
	_gLogger.event(_gLogger.skip+1, LevelFatal, nil, fmt.Sprintf(format, args...), true).Object(obj).Println()
}
